home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 101-125 / scopedisk122 / bassub / tools.sub < prev    next >
Text File  |  1995-03-19  |  11KB  |  534 lines

  1. 'Numerous subroutines for handling various chores in programs
  2. REM FNstr$
  3. 'converts number to string with rounding control
  4. 'num = number to be converted
  5. 'decimal = number of decimal places to include in returned string
  6. DEF FNstr$(num,decimal)
  7.     digits_left=FIX(num)
  8.     digits_right=FNfrac(num)
  9.     digitr$=LEFT$(STR$(digits_right),decimal+2)
  10.     digitl$=STR$(digits_left)
  11.     FNstr$=FNstrip$(digitl$+digitr$)
  12. END DEF
  13. REM FNfrac
  14. 'returns the fractional component of number (Like TRUNC)
  15. DEF FNfrac(x)
  16.     FNfrac=x-FIX(x)
  17. END DEF
  18. REM divide
  19. 'returns quotient and remainder of a division of two numbers
  20. 'divisor is denominator
  21. 'dividend is numerator
  22. SUB divide(divisor,dividend,quotient,rmainder)
  23.     quotient=divisor\dividend
  24.     rmainder=divisor-dividend*quotient
  25. END SUB
  26. REM Pause
  27. 'Delay routine 
  28. SUB Pause(delay)
  29.     t!=TIMER
  30.     WHILE TIMER < t!+delay
  31.     WEND
  32. END SUB
  33. REM center.msg
  34. 'print a centered message in the window
  35. 'msg$ = message
  36. 'line.num% = line to display message
  37. 'wid% = width of window in pixels
  38. SUB center.msg(msg$,line.num%,wid%)
  39.   LOCATE line.num%,1
  40.   PRINT STRING$(wid%/8," ");
  41.   LOCATE line.num%,INT((wid%/8-LEN(msg$))/2)
  42.   PRINT msg$
  43. END SUB
  44. REM blink
  45. 'display a blinking message, a$ at blink rate, speed
  46. SUB blink(a$,speed)
  47.   x%=CRSLIN
  48.   y%=POS(0)
  49.   FOR i%=1 TO 10
  50.     LOCATE y%,x%
  51.     PRINT a$;
  52.     CALL PAUSE(speed)
  53.     LOCATE y%,x%
  54.     PRINT STRING$(LEN(a$)," ");
  55.     CALL PAUSE(speed)
  56.   NEXT i%
  57.   LOCATE y%,x%
  58.   PRINT a$
  59. END SUB
  60. REM removestr
  61. 'remove substring, substr$ from string, s$
  62. SUB removestr(s$,substr$)
  63.   k%=LEN(substr$)
  64.   l%=INSTR(s$,substr$)
  65.   WHILE l%>0
  66.     IF l%=1
  67.       s$=MID$(s$,(l%+k%))
  68.     ELSE
  69.       s$=MID$(s$,1,(l%-1))+MID$(s$,(l%+k%))
  70.     END IF
  71.     l%=INSTR(s$,substr$)
  72.   WEND
  73. END SUB
  74. REM FNpadleft$
  75. 'pad left of string, s$ with l% occurances of character, char$
  76. DEF FNpadleft$(s$,char$,l%)
  77.   IF l%<1
  78.     l%=1
  79.   END IF
  80.   IF LEN(char$)>1
  81.     char$=LEFT$(char$,1)
  82.   END IF
  83.   FNpadleft$=STRING$(l%,char$)+s$
  84. END DEF
  85. REM FNpadright$
  86. 'pad right of string, s$ with l% occurances of character, char$
  87. DEF FNpadright$(s$,char$,l%)
  88.   IF l%<1
  89.     l%=1
  90.   END IF
  91.   IF LEN(char$)>1
  92.     char$=LEFT$(char$,1)
  93.   END IF
  94.   FNpadright$=s$+STRING$(l%,char$)
  95. END DEF
  96. REM FNpadcenter$
  97. 'pad center of string, s$, beginning at index%, with l% occurances of
  98. 'character, char$
  99. DEF FNpadcenter$(s$,char$,l%,index%)
  100.   IF l%<1
  101.     l%=1
  102.   END IF
  103.   IF index%<1
  104.     index%=2
  105.   END IF
  106.   IF index%<LEN(s$)
  107.     IF LEN(char$)>1
  108.       char$=LEFT$(char$,1)
  109.     END IF
  110.     FNpadcenter$=LEFT$(s$,index%-1)+STRING$(l%,char$)+MID$(s$,index%)
  111.   ELSE
  112.     FNpadcenter$=""
  113.   END IF
  114. END DEF
  115. REM FNpadends$
  116. 'pad both ends of string, s$ with l% occurances of character, char$
  117. DEF FNpadends$(s$,char$,l%)
  118.   IF l%<1
  119.     l%=1
  120.   END IF
  121.   t$=STRING$(l%,char$)
  122.   FNpadends$=t$+s$+t$
  123. END DEF
  124. REM FNdelleft$
  125. 'delete character to left of char$ in string, s$
  126. ' If killc%<>0 then char$ also deleted.
  127. DEF FNdelleft$(s$,char$,killc%)
  128.   IF LEN(char$)>1
  129.     char$=LEFT$(char$,1)
  130.   END IF
  131.   l%=INSTR(s$,char$)
  132.   IF l%=0
  133.     FNdelleft$=s$
  134.   ELSEIF (killc%=0) THEN
  135.     DECR l%
  136.   END IF
  137.   FNdelleft$=MID$(s$,l%+1)
  138. END DEF
  139. ' Delete all right of first occurance of char$.  Delete char$ if killc%<>0
  140. REM FNdelright$
  141. DEF FNdelright$(s$,char$,killc%)
  142.   IF LEN(char$)>1
  143.     char$=LEFT$(char$,1)
  144.   END IF
  145.   l%=LEN(s$)
  146.   WHILE (MID$(s$,l%,1)<>char$) AND (l%>0)
  147.     DECR l%
  148.   WEND
  149.   IF l%=0
  150.     FNdelright$=s$
  151.   ELSEIF (killc%<>0) THEN
  152.     DECR l%
  153.   END IF
  154.   FNdelright$=LEFT$(s$,l%)
  155. END DEF
  156. REM FNnum.in.range
  157. 'determine if number, a, is in range of low and hi
  158. DEF FNnum.in.range(msg$,low,hi)
  159.   wherey%=CRSLIN
  160.   wherex%=POS(0)
  161.   DO
  162.     LOCATE wherey%,wherex%
  163.     PRINT STRING$((80-wherex%)," ");
  164.     LOCATE wherey%,wherex%
  165.     PRINT msg$;
  166.     INPUT " ";a
  167.   LOOP UNTIL (a>=low) AND (a<=hi)
  168.   FNnum.in.range=a
  169. END DEF
  170. REM FNinteger.in.range%
  171. 'determine if integer a% is in range of low% and hi%
  172. DEF FNinteger.in.range%(msg$,low%,hi%)
  173.   wherey%=CRSLIN
  174.   wherex%=POS(0)
  175.   DO
  176.     LOCATE wherey%,wherex%
  177.     PRINT STRING$((80-wherex%)," ");
  178.     LOCATE wherey%,wherex%
  179.     PRINT msg$;
  180.     INPUT " ";a%
  181.   LOOP UNTIL (a%>=low%) AND (a%<=hi%)
  182.   FNinteger.in.range%=a%
  183. END DEF
  184. REM FNyesno$
  185. 'get a yes/no response
  186. DEF FNyesno$(msg$)
  187.   DO
  188.     LOCATE CRSLIN,POS(0)
  189.     PRINT msg$;
  190.     INPUT " (Y/N) ";a$
  191.     IF LEN(a$)>0
  192.       a$=UCASE$(LEFT$(a$,1))
  193.     END IF
  194.   LOOP UNTIL (a$="N") OR (a$="Y")
  195.   FNyesno$=a$
  196. END DEF
  197. REM FNstrip$
  198. 'strips blanks from string, s$
  199. DEF FNstrip$(s$)
  200.     l%=LEN(s$)
  201.     t$=""
  202.     FOR i%=1 TO l%
  203.         c$=MID$(s$,i%,1)
  204.         IF c$<>" " THEN
  205.             t$=t$+c$
  206.         END IF
  207.     NEXT i%
  208.     FNstrip$=t$
  209. END DEF
  210. REM FNRstrip$
  211. 'strips blanks at right of string, s$
  212. DEF FNRstrip$(s$)
  213.     l%=LEN(s$)
  214.     FOR i%=l% TO 1 STEP -1
  215.         c$=MID$(s$,i%,1)
  216.         IF c$<>" " THEN
  217.             index%=i%
  218.             EXIT FOR
  219.         END IF
  220.     NEXT i%
  221.     FNRstrip$=LEFT$(s$,index%)
  222. END DEF
  223. REM FNLstrip$
  224. 'strips blanks at left of string, s$
  225. DEF FNLstrip$(s$)
  226.     l%=LEN(s$)
  227.     FOR i%=1 TO l%
  228.         c$=MID$(s$,i%,1)
  229.         IF c$<>" " THEN
  230.             index%=i%
  231.             EXIT FOR
  232.         END IF
  233.     NEXT i%
  234.     FNLstrip$=RIGHT$(s$,l%-index%+1)
  235. END DEF
  236. REM FNcheck_num%
  237. 'check if string, s$ is an integer
  238. DEF FNcheck_num%(s$)
  239.     l%=LEN(s$)
  240.     FOR i%=1 TO l%
  241.         IF FNisnumber%(MID$(s$,i%,1)) <> TRUE% THEN
  242.             FNcheck_num%=FALSE%
  243.             EXIT SUB
  244.         ELSE
  245.             FNcheck_num%=TRUE%
  246.         END IF
  247.     NEXT i%
  248. END DEF
  249. REM FNischar%
  250. 'check if s$ is a printable character
  251. DEF FNischar%(s$)
  252.     IF ASC(s$)>=&H21 AND ASC(s$)<=&H7E THEN
  253.         FNischar%=TRUE%
  254.     ELSE
  255.         FNischar%=FALSE%
  256.     END IF
  257. END DEF
  258. REM FNisnumber%
  259. 'check if s$ is an integer
  260. DEF FNisnumber%(s$)
  261.     IF ASC(s$)>=48 AND ASC(s$)<=57 THEN
  262.         FNisnumber%=TRUE%
  263.     ELSE
  264.         FNisnumber%=FALSE%
  265.     END IF
  266. END DEF
  267. REM FNeven
  268. 'check if num% is even number
  269. DEF FNeven(num%)
  270.     IF num% MOD 2 = 0 THEN
  271.         FNeven = FALSE%
  272.     ELSE
  273.         FNeven = TRUE%
  274.     END IF
  275. END DEF
  276. REM FNodd
  277. 'check if num% is an odd number
  278. DEF FNodd(num%)
  279.     IF num% MOD 2 <> 0 THEN
  280.         FNodd = FALSE%
  281.     ELSE
  282.         FNodd = TRUE%
  283.     END IF
  284. END DEF
  285. REM FNRound!
  286. 'round num# to digits%
  287. DEF FNRound!(num#,digits%)
  288.     FNRound!=CLNG(num#*(10^digits%)+.5)/(10^digits%)
  289. END DEF
  290. REM FNgetword$
  291. 'extract the getnum% word from string, s$ using delimiting string, delim$
  292. DEF FNgetword$(s$,delim$,getnum%)
  293.       IF getnum%<1 THEN getnum%=1
  294.       IF delim$="" THEN delim$=" "
  295.       
  296.       n%=getnum%-1
  297.       i%=1
  298.       l%=LEN(s$)
  299.       
  300.       IF n%=0 THEN
  301.           WHILE (i%<l%) AND (INSTR(delim$,MID$(s$,i%,1))=0)
  302.               INCR i%
  303.           WEND
  304.           ptr1%=i%
  305.           FNgetword$=MID$(s$,1,ptr1%)
  306.           EXIT DEF
  307.       END IF
  308.       
  309.       WHILE (i%<l%) AND (INSTR(delim$,MID$(s$,i%,1))>0)
  310.             INCR i%
  311.       WEND
  312.       IF i%>l%
  313.             FNgetword$=""
  314.             EXIT DEF
  315.       END IF
  316.       IF n%>0 THEN INCR i%
  317.       
  318.       WHILE (i%<=l%) AND (n%>0)
  319.             IF (FNtestdelim%(s$,delim$,i%)=1) THEN DECR n%
  320.             INCR i%
  321.       WEND
  322.       
  323.       IF (n%>0)
  324.             FNgetword$=""
  325.       ELSE
  326.             WHILE (i%<l%) AND (INSTR(delim$,MID$(s$,i%,1))>0)
  327.               INCR i%
  328.             WEND
  329.             ptr1%=i%
  330.             n%=1
  331.             WHILE (i%<=l%) AND (n%>0)
  332.               IF (FNtestdelim%(s$,delim$,i%)=1) THEN DECR n%
  333.               INCR i%
  334.             WEND
  335.             
  336.             FNgetword$=MID$(s$,ptr1%,i%-ptr1%)
  337.       END IF
  338. END DEF
  339. REM FNwordpos
  340. 'return the position value of search$ string in string, s$ starting at index%
  341. 'strings are delimited by string, delim$
  342. DEF FNwordpos(s$,delim$,search$,index%)
  343.       IF (INSTR(s$,search$)=0)
  344.             FNwordpos=0
  345.       END IF
  346.       IF delim$=""
  347.             delim$=" "
  348.       END IF
  349.       l%=LEN(s$)
  350.       n%=index%-1
  351.       i%=1
  352.       WHILE (i%<l%) AND (INSTR(delim$,MID$(s$,i%,1))>0)
  353.             INCR i%
  354.       WEND
  355.       IF i%>l%
  356.             FNwordpos=0
  357.       END IF
  358.       IF n%>0
  359.             INCR i%
  360.       END IF
  361.       WHILE (i%<=l%) AND (n%>0)
  362.             IF FNtestdelim%(s$,delim$,i%)=1
  363.               DECR n%
  364.             END IF
  365.             INCR i%
  366.       WEND
  367.       IF n%>0
  368.             FNwordpos=0
  369.       ELSE
  370.             IF i%>1
  371.               DECR i%
  372.             END IF
  373.             ptr%=INSTR(i%,s$,search$)
  374.             count%=1
  375.             WHILE (i%<ptr%)
  376.               IF FNtestdelim%(s$,delim$,i%)=1
  377.                     INCR count%
  378.               END IF
  379.               INCR i%
  380.             WEND
  381.             FNwordpos=count%+index%-1
  382.       END IF
  383. END DEF
  384. REM FNdelword$
  385. 'delete count% word in string, s$, starting from start%
  386. 'using delimiting string, delim$
  387. DEF FNdelword$(s$,delim$,start%,count%)
  388.       IF delim$=""
  389.             delim$=" "
  390.       END IF
  391.       l%=LEN(s$)
  392.       n%=start%-1
  393.       i%=1
  394.       WHILE (i%<l%) AND (INSTR(delim$,MID$(s$,i%,1))>0)
  395.             INCR i%
  396.       WEND
  397.       IF n%>0
  398.             INCR i%
  399.       END IF
  400.       WHILE (i%<=l%) AND (n%>0)
  401.             IF FNtestdelim%(s$,delim$,i%)=1
  402.               DECR n%
  403.             END IF
  404.             INCR i%
  405.       WEND
  406.       IF (n%=0)
  407.             WHILE (i%<l%) AND (INSTR(delim$,MID$(s$,i%,1))>0)
  408.               INCR i%
  409.             WEND
  410.             ptr1%=i%-1
  411.             n%=count%
  412.             WHILE (i%<=l%) AND (n%>0)
  413.               IF FNtestdelim%(s$,delim$,i%)=1
  414.                     DECR n%
  415.               END IF
  416.               INCR i%
  417.             WEND
  418.             IF (n%>0) OR (i%>l%)
  419.               FNdelword$=LEFT$(s$,ptr1%-1)
  420.             ELSE
  421.               FNdelword$=LEFT$(s$,ptr1%)+MID$(s$,i%)
  422.             END IF
  423.       END IF
  424. END DEF
  425. REM FNinsertword$
  426. 'insert newword$ starting at index% in string, s$
  427. 'words are delimited by delimiting string, delim$
  428. DEF FNinsertword$(s$,delim$,newword$,index%)
  429.       IF delim$=""
  430.             delim$=" "
  431.       END IF
  432.       l%=LEN(s$)
  433.       n%=index%
  434.       i%=1
  435.       WHILE (i%<l%) AND (INSTR(delim$,MID$(s$,i%,1))>0)
  436.             INCR i%
  437.       WEND
  438.       IF n%>0
  439.             INCR i%
  440.       END IF
  441.       WHILE (i%<=l%) AND (n%>0)
  442.             IF FNtestdelim%(s$,delim$,i%)=1
  443.               DECR n%
  444.             END IF
  445.             INCR i%
  446.       WEND
  447.       IF (n%=0)
  448.             ptr%=i%-1
  449.             FNinsertword$=LEFT$(s$,ptr%)+newword$+MID$(s$,ptr%)
  450.       END IF
  451. END DEF
  452. REM FNtestdelim%
  453. 'utility routine used by some of these subroutines
  454. DEF FNtestdelim%(s$,delim$,i%)
  455.       IF (INSTR(delim$,MID$(s$,i%-1,1))=0) AND (INSTR(delim$,MID$(s$,i%,1))>0)
  456.             FNtestdelim%= 1
  457.       ELSE
  458.             FNtestdelim%= 0
  459.       END IF
  460. END DEF
  461. '
  462. 'The following are useful routines for taking line input from a file
  463. 'and breaking up the line into strings or numbers.  Exploden may be
  464. 'changed to provide a third subroutine for integers or the numbers
  465. 'in array may be changed to integers later in the main program
  466. REM exploden
  467. 'breaks a string of numbers, s$, delimited by delimiting string, delim$, into
  468. 'numbers in the array nums#()
  469. 'returns numbers in nums#() and the number of values in count%
  470. SUB exploden(s$,delim$,nums#(1),count%)
  471.       l%=LEN(s$)
  472.       IF l%=0 THEN
  473.             EXIT SUB
  474.       END IF
  475.       i%=1
  476.       count%=0
  477.       WHILE (i%<l%) AND (INSTR(delim$,MID$(s$,i%,1))>0)
  478.             INCR i%
  479.       WEND
  480.       ptr1%=i%
  481.       IF (i%=1) THEN
  482.             i%=2
  483.       END IF
  484.       WHILE i%<=l%
  485.             IF FNtestdelim%(s$,delim$,i%)=1 THEN
  486.               INCR count%
  487.               ptr2%=i%-1
  488.               nums#(count%)=VAL(MID$(s$,ptr1%,ptr2%-ptr1%+1))
  489.               WHILE (i%<l%) AND (INSTR(delim$,MID$(s$,i%,1))>0)
  490.                     INCR i%
  491.               WEND
  492.               ptr1%=i%
  493.             END IF
  494.             INCR i%
  495.       WEND
  496.       IF ptr1%<l% THEN
  497.             INCR count%
  498.             nums#(count%)=VAL(MID$(s$,ptr1%))
  499.       END IF
  500. END SUB
  501. REM explodes
  502. 'Same as exploden, except that strings are placed in array words$()
  503. SUB explodes(s$,delim$,words$(1),count%)
  504.       l%=LEN(s$)
  505.       IF l%=0 THEN
  506.             EXIT SUB
  507.       END IF
  508.       i%=1
  509.       count%=0
  510.       WHILE (i%<l%) AND (INSTR(delim$,MID$(s$,i%,1))>0)
  511.             INCR i%
  512.       WEND
  513.       ptr1%=i%
  514.       IF (i%=1) THEN
  515.             i%=2
  516.       END IF
  517.       WHILE i%<=l%
  518.             IF FNtestdelim%(s$,delim$,i%)=1 THEN
  519.               INCR count%
  520.               ptr2%=i%-1
  521.               words$(count%)=MID$(s$,ptr1%,ptr2%-ptr1%+1)
  522.               WHILE (i%<l%) AND (INSTR(delim$,MID$(s$,i%,1))>0)
  523.                     INCR i%
  524.               WEND
  525.               ptr1%=i%
  526.             END IF
  527.             INCR i%
  528.       WEND
  529.       IF ptr1%<l% THEN
  530.             INCR count%
  531.             words$(count%)=MID$(s$,ptr1%)
  532.       END IF
  533. END SUB
  534.